package com.mooninterface.controller;

import com.moon.moonclientsdk.model.User;
import com.moon.moonclientsdk.util.SignUtil;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/name")
public class NameController {

    @GetMapping("/get")
    public String getNameByGet(String name, HttpServletRequest request){
        System.out.println(request.getHeader("moon"));
        System.out.println("get你的名字："+name);
        return "getNameByGet接口-get你的名字："+name;
    }

    @PostMapping("/post")
    public String getNameByPost(@RequestParam String name){
        return "getNameByPost接口-post你的名字："+name;
    }

    @PostMapping("/user")
    public String getUserNameByPost(@RequestBody User user, HttpServletRequest request){
        //String accessKey=request.getHeader("accessKey");
        //String nonce=request.getHeader("nonce");
        //String timestamp=request.getHeader("timestamp");
        //String sign=request.getHeader("sign");
        //String body=request.getHeader("body");
        ////TODO 实际是去数据库查是否已分配给用户
        //if (!accessKey.equals("moon")){
        //    throw new RuntimeException("无权限");
        //}
        ////这里不校验随机数了
        //if (Long.parseLong(nonce)>10000) {
        //    throw new RuntimeException("无权限");
        //}
        ////TODO 时间戳：时间和当前时间不超过5分钟
        ////if(timestamp){}
        ////TODo 实际情况是从数据库中查询出 secretKey
        //String serverSign=SignUtil.getSign(body,"abcdefg");
        //if (!sign.equals(serverSign)){
        //    throw new RuntimeException("无权限");
        //}
        //调用成功后次数+1
        return "post用户的名字："+user.getUsername();
    }
}
